﻿//Write a program that finds the index of given element in a sorted array of integers by using the binary search algorithm (find it in Wikipedia).


using System;

namespace Binaryearch
{
    class Binaryearch
    {
        static void Main()
        {
            int[] arr = { -10, -8, -6, -4, -2, 0, 1, 3, 5, 7, 9 }; //initializing the arr
            Console.Write("Your number is: ");      //ask for the users choise
            int value = int.Parse(Console.ReadLine()),
                low = 0,
                high = arr.Length - 1,
                midpoint = 0;

            while (low <= high) //use of the binary search
            {
                midpoint = low + (high - low) / 2;

                if (value == arr[midpoint])
                {
                    Console.WriteLine("Your position is: " + midpoint); //print the result if there is such num 
                    break;
                }
                else if (value < arr[midpoint])
                {
                    high = midpoint - 1;
                }
                else
                {
                    low = midpoint + 1;
                }
            }

            if (low > high)
            {
                Console.WriteLine("There is No such number in the array.");
            }
        }
    }
}
